home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbuts.zip / COMBDUMP.C < prev    next >
C/C++ Source or Header  |  1991-05-16  |  6KB  |  251 lines

  1. /*
  2.  * COMBINEDUMP.C
  3.  *
  4.  * Written by Ville Saari
  5.  *
  6.  * Copyright (c) 1991 Ferry Island Pixelboys
  7.  * All rights reserved
  8.  *
  9.  * Created: 06-Jan-91
  10.  * Updated: 31-Jan-91
  11.  *
  12.  * Updated: 07-May-91 - Aaron A. Collins - Made somewhat more portable, and
  13.  * made the program write out to a file instead of stdout.
  14.  */
  15.  
  16. #define VERSION "1.10"
  17.  
  18. #define COPYRIGHT \
  19.    "\033[33;1mCOMBINEDUMP\033[0m V" VERSION " by Ville Saari.\n"\
  20.    "Copyright (c) 1991 Ferry Island Pixelboys.\n"\
  21.    "Freeware.\n"
  22.  
  23. #define USAGE \
  24.    "\n" \
  25.    "Usage: combinedump [-l(R|G|B)] [-r(R|G|B)]"\
  26.    " [-c((l|r)((t|b)<value>)...)...]\n"\
  27.    "                  <leftfile> <rightfile> <outfile>\n\n"\
  28.    "Defaults: -lR -rG -clt34b85rt0b51\n"
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33.  
  34. #define R 0
  35. #define G 1
  36. #define B 2
  37. #define BUFSIZE 16384L
  38.  
  39. FILE *lfile = NULL, *rfile = NULL, *ofile = NULL;
  40.  
  41. long left=R, lbot=85, ltop=34;
  42. long right=G, rbot=51, rtop=0;
  43.  
  44. long readval(FILE *file)
  45.    {
  46.    long x;
  47.  
  48.    x=fgetc(file);
  49.    x|=fgetc(file)<<8;
  50.  
  51.    return x;
  52.    }
  53.  
  54. void writeval(long x, FILE *file)
  55.    {
  56.    putc((char)x, file);
  57.    putc((char)(x>>8), file);
  58.    }
  59.  
  60. long getrgb(char ch)
  61.    {
  62.    switch(ch|0x20)
  63.       {
  64.       case 'r':
  65.          return R;
  66.       case 'g':
  67.          return G;
  68.       case 'b':
  69.          return B;
  70.       default:
  71.          return -1;
  72.       }
  73.    }
  74.  
  75. void error(char *text, int code)
  76.    {
  77.    if(code) fputs("COMBINEDUMP: ", stderr);
  78.    fputs(text, stderr);
  79.    fflush(stderr);
  80.    if (ofile != NULL)
  81.       {
  82.       fflush(ofile);
  83.       fclose(ofile);
  84.       }
  85.    if (lfile != NULL)
  86.       fclose(lfile);
  87.    if (rfile != NULL)
  88.       fclose(rfile);
  89.    exit(code);
  90.    }
  91.  
  92. void main(int ac, char **arg)
  93.    {
  94.    char *lname=NULL, *rname=NULL, *oname=NULL;
  95.    unsigned char *lbuf, *rbuf;
  96.    long f, n, l, r, t, b, val;
  97.    long width, height, buflines, bufsize, linesize, bufplace;
  98.    long lcol, rcol;
  99.    long lbc, rbc, lsc, rsc;
  100.    long ofs[3];
  101.  
  102.    fputs(COPYRIGHT, stderr);
  103.  
  104.    if(ac < 4 || arg[1][0] == '?')
  105.        error(USAGE, 0);
  106.  
  107.    for(f=1; f<ac; f++)
  108.       {
  109.       if(arg[f][0]!='-')
  110.          {
  111.          if(!lname) lname=arg[f];
  112.          else
  113.             {
  114.             if(!rname) rname=arg[f];
  115.             else
  116.                 {
  117.                 if(!oname) oname=arg[f];
  118.                 else error("Too many arguments.\n", 20);
  119.                 }
  120.             }
  121.         }
  122.       else
  123.          {
  124.          switch(arg[f][1])
  125.             {
  126.             case 'l':
  127.                if((left=getrgb(arg[f][2]))<0)
  128.                   error("Illegal '-l' switch.\n", 20);
  129.                break;
  130.             case 'r':
  131.                if((right=getrgb(arg[f][2]))<0)
  132.                   error("Illegal '-r' switch.\n", 20);
  133.                break;
  134.             case 'c':
  135.                n=2, l=r=t=b=0;
  136.                while(arg[f][n])
  137.                   {
  138.                   switch(arg[f][n])
  139.                      {
  140.                      case 'l':
  141.                         l=1, r=0, n++;
  142.                         break;
  143.                      case 'r':
  144.                         l=0, r=1, n++;
  145.                         break;
  146.                      case 't':
  147.                         t=1, b=0, n++;
  148.                         break;
  149.                      case 'b':
  150.                         t=0, b=1, n++;
  151.                         break;
  152.                      default:
  153.                         if(isdigit(arg[f][n])) val=0; else val=-1;
  154.                         for(; isdigit(arg[f][n]); n++)
  155.                            val=10*val+arg[f][n]-'0';
  156.                         if(!l && !r || !t && !b || val<0 || val>255)
  157.                            error("Illegal '-c' switch.\n", 20);
  158.                         if(l && b) lbot=val;
  159.                         if(l && t) ltop=val;
  160.                         if(r && b) rbot=val;
  161.                         if(r && t) rtop=val;
  162.                      }
  163.                   }
  164.                break;
  165.             default:
  166.                error("Illegal switch.\n", 20);
  167.             }
  168.          }
  169.       }
  170.  
  171.    if(!(lfile=fopen(lname, "rb")))
  172.       error("Can't open left input file.\n", 20);
  173.  
  174.    if(!(rfile=fopen(rname, "rb")))
  175.       error("Can't open right input file.\n", 20);
  176.  
  177.    if(!(ofile=fopen(oname, "wb")))
  178.       error("Can't open output file.\n", 20);
  179.  
  180.    if((width=readval(lfile))!=readval(rfile) ||
  181.       (height=readval(lfile))!=readval(rfile))
  182.          error("Input files are incompatible.\n", 20);
  183.  
  184.    linesize=2+3*width;
  185.  
  186.    if(!(buflines=BUFSIZE/linesize))
  187.       error("Too long input lines.\n", 20);
  188.  
  189.    bufsize=buflines*linesize;
  190.  
  191.    if(!(lbuf=malloc((unsigned int)(buflines*linesize))) ||
  192.       !(rbuf=malloc((unsigned int)(buflines*linesize))))
  193.           error("Can't allocate memory for buffers.\n", 20);
  194.  
  195.    writeval(width, ofile);
  196.    writeval(height, ofile);
  197.  
  198. #ifndef NOANSICODES
  199.    fprintf(stderr, "\033[0mProcessing line:      of %-5d\033[14D", (int)height);
  200. #endif
  201.  
  202.    for(f=0, bufplace=0; f<height; f++)
  203.       {
  204.       if(bufplace==0)
  205.          if(!fread((char *)lbuf, (unsigned int)linesize, (unsigned int)buflines, lfile) ||
  206.             !fread((char *)rbuf, (unsigned int)linesize, (unsigned int)buflines, rfile))
  207.                 error("Input files are incomplete.\n", 20);
  208.  
  209.       if(lbuf[bufplace]!=rbuf[bufplace] ||
  210.          lbuf[bufplace+1]!=rbuf[bufplace+1])
  211.              error("Line numbers don't match.\n", 20);
  212.  
  213. #ifndef NOANSICODES
  214.       fprintf(stderr, "%5d\033[5D", (int)f+1);
  215. #endif
  216.  
  217.       ofs[0]=bufplace+2; ofs[1]=bufplace+2+width; ofs[2]=bufplace+2+2*width;
  218.  
  219.       for(n=0; n<width; n++)
  220.          {
  221.          lcol=lbuf[ofs[2]]+(lbuf[ofs[0]]<<1)+(lbuf[ofs[1]]<<2);
  222.          rcol=rbuf[ofs[2]]+(rbuf[ofs[0]]<<1)+(rbuf[ofs[1]]<<2);
  223.  
  224.          rbc=(1785-lcol)*rbot/1786;
  225.          lbc=(1785-rcol)*lbot/1786;
  226.          rsc=255-lcol*rtop/1786-rbc;
  227.          lsc=255-rcol*ltop/1786-lbc;
  228.  
  229.          lcol=lbc+lcol*lsc/1786;
  230.          rcol=rbc+rcol*rsc/1786;
  231.  
  232.          lbuf[ofs[0]]=lbuf[ofs[1]]=lbuf[ofs[2]]=0;
  233.  
  234.          lbuf[ofs[left]]=(char)lcol;
  235.          lbuf[ofs[right]]=(char)rcol;
  236.  
  237.          ofs[0]++, ofs[1]++, ofs[2]++;
  238.          }
  239.  
  240.       if((bufplace+=linesize)>=bufsize || f==height-1)
  241.          {
  242.          if(!(fwrite(lbuf, (unsigned int)bufplace, 1, ofile)))
  243.              error("Error when writing output file.\n", 20);
  244.          bufplace=0;
  245.          }
  246.       }
  247. #ifndef NOANSICODES
  248.    fputs("\033[0m\n", stderr);
  249. #endif
  250.    }
  251.